package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.controller;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LinkStrategy extends NodeStrategy {
    public static final String LinkRouteCondition_True = "LinkRouteCondition_True";
    public static final String LinkRouteCondition_False = "LinkRouteCondition_False";

    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto) throws Exception {
        String linkRouteCondition = node.getLinkRouteCondition();
        if (!StringUtils.isNull(linkRouteCondition)) {
            log.info("LinkStrategy linkRouteCondition: {}", linkRouteCondition);
        }

        boolean b = false;
        String flag = LinkRouteCondition_False;
        if (StringUtils.isNull(linkRouteCondition)) {
            flag = LinkRouteCondition_False;
        }
        else if ("false".equalsIgnoreCase(linkRouteCondition)) {
            flag = LinkRouteCondition_False;
        }
        else if ("true".equalsIgnoreCase(linkRouteCondition)) {
            flag = LinkRouteCondition_True;
        }else{
            b = (Boolean) executeScriptForResult(dataDto, linkRouteCondition);
            if (b) {
                flag = LinkRouteCondition_True;
            } else {
                flag = LinkRouteCondition_False;
            }
        }


        // 数据转换
        /*List<Map<String, Object>> dataList = getDataList(dataDto);
        if (dataList==null || dataList.size()==0) {
            return Result.builder(flag);
        }

        // 映射关系 mapJson
        JSONArray inOutMapArray = JSONArray.parseArray(node.getMapJson());
        // 不用转换
        if (inOutMapArray==null || inOutMapArray.size()==0) {
            return Result.builder(flag);
        }
        // 转换变量
        for (Map<String,Object> dataMap:dataList) {
            for(int i=0;i<inOutMapArray.size();i++){
                JSONObject inOutMap = inOutMapArray.getJSONObject(i);
                String in = inOutMap.getString("in");
                String out = inOutMap.getString("out");
                dataMap.put(out, dataMap.get(in));
                dataMap.remove(in);
            }
        }*/

        return Result.builder(flag);
    }
}
